<?php
/** Copyright 2014 TinPhp Systems, Inc.
 * @author zhangxilian 2013-8-15
 */
if (!defined('IN_TINPHP')) {
	exit ('Access Denied');
}

/**
 * 管理员帐号业务逻辑类
 * 
 * 每个 Logic 对应一个业务逻辑处理单元、必须继承自 BaseLogic
 * 每个 Logic 对应一个或多个 View/DAO
 */
class UserLogic extends TinBaseLogic {

	private $userDAO;
	private $userTypeDAO;

	// 每个 Logic 需要有一个构造函数
	public function UserLogic() {        
		parent::__construct();
		$this->logger->className = "UserLogic";
		$this->userDAO = Tin::getDAO("AssignTable","USER");
		$this->userTypeDAO = Tin::getDAO("AssignTable","USER_TYPE");
        $this->userInfoDAO = Tin::getDAO("AssignTable","USER_INFO");
		$this->userView = Tin::getDAO("AssignView","V_USERINFO");
	}
	
	public function __destruct(){
		$this->userDAO = null;
		$this->userTypeDAO = null;
	}
//=========================================用户=============================================================
	/** 指定获取单条用户记录
	 * @author ZhangXiLian 2014-8-29 */
	public function getUserItem($cols, $cond, $params = false){
		return $this->userDAO->selectRowByCond($cols, $cond, $params);
	}
	/** 根据指定条件分页获取用户列表数据 
	 * @author ZhangXiLian 2014-8-29 */
	public function getUserList($cols, $cond, $order_by  = null, $page = null, $size = null){
        $res = array();
		$res["data"] = $this->userDAO->selectByCond($cols, $cond . " " . $order_by, $page, $size);
		if ($page){
			$res["count"] = $this->userDAO->getCountByCond($cond);
		}
		return $res;
	}
	/** 根据条件获取会员等级列表 */
	public function getUserTypeList($cols, $cond, $order_by  = null, $page = null, $size = null){
        $res = array();
		$res["data"] = $this->userTypeDAO->selectByCond($cols, $cond . " " . $order_by, $page, $size);
		if ($page){
			$res["count"] = $this->userTypeDAO->getCountByCond($cond);
		}
		return $res;
	}
	/** 根据条件获取会员列表 */
	public function getUserInfoList($cols, $cond, $order_by  = null, $page = null, $size = null){
        $res = array();
		$res["data"] = $this->userView->selectByCond($cols, $cond . " " . $order_by, $page, $size);
		if ($page){
			$res["count"] = $this->userView->getCountByCond($cond);
		}
		return $res;
	}
	/** 添加用户记录
	 * @author ZhangXiLian 2014-8-29 */
	public function addUserItem($data){		
		$map = Tin :: createDataMap();
		$map->addEntry('USERNAME', $data['username'], DB::VARCHAR);
		$map->addEntry('PASSWORD', $data['password'], DB::VARCHAR);
		$map->addEntry('USERTYPE_ID', $data['usertype_id'] ? $data['usertype_id'] : 1, DB::INT);
		$map->addEntry('LOGIN_NUM', 0, DB::INT);
		$map->addEntry('CREATE_DATE', 'now()', DB::DATETIME);
		try{
			$newUserID = $this->userDAO->insert($map, false, true);				
			return $newUserID;
		}catch(Exception $e){
            $this->logger->error("addUserItem:".$e->getMessage());
			return false;
		}
	}

	/** 添加用户记录
	 * @author ZhangXiLian 2014-8-29 */
	public function addUserInfo($data){		
		$map = Tin :: createDataMap();
		if($data['realname']) $map->addEntry('REALNAME', $data['realname'], DB::VARCHAR);
		if($data['idcard']) $map->addEntry('IDCARD', $data['idcard'], DB::VARCHAR);
		if($data['username']) $map->addEntry('TEL', $data['username'], DB::VARCHAR);
		if($data['geo_code']) $map->addEntry('GEO_CODE', $data['geo_code'], DB::VARCHAR);
		if($data['address']) $map->addEntry('ADDRESS', $data['address'], DB::VARCHAR);
		if($data['sex']) $map->addEntry('SEX', $data['sex'], DB::VARCHAR);
		if($data['email']) $map->addEntry('EMAIL', $data['email'], DB::VARCHAR);
		$map->addEntry('USERID', $data['userid'], DB::VARCHAR);
		$map->addEntry('STATE', 0, DB::INT);
		try{
			$newUserID = $this->userInfoDAO->insert($map, false, true);				
			return $newUserID;
		}catch(Exception $e){
            $this->logger->error("addUserInfo:".$e->getMessage());
			return false;
		}
	}
	public function getUserInfo($cols, $cond, $params = false){
		return $this->userInfoDAO->selectRowByCond($cols, $cond, $params);
	}


	/** 根据条件更新指定字段
	 * @param array $dataArr 要更新的字段值数组
	 * @param string $cond 条件
	 * @param array $params 绑定变量
	 * @author ZhangXiLian 2014-8-29 */
	public function updateFieldVal($dataArr, $cond, $params=false){
		if(!is_array($dataArr))return false;
		$map = Tin :: createDataMap();
		foreach ($dataArr as $k=>$v){
			$map->addEntry(strtoupper($k), $v, DB::VARCHAR);
		}
		try{
			return $this->userDAO->updateByCond($map, $cond, false, $params);
		}catch(Exception $e){
            $this->logger->error("updateFieldVal:".$e->getMessage());
			return false;
		}
	}
	public function updateInfoVal($dataArr, $cond, $params=false){
		if(!is_array($dataArr))return false;
		$map = Tin :: createDataMap();
		foreach ($dataArr as $k=>$v){
			$map->addEntry(strtoupper($k), $v, DB::VARCHAR);
		}
		try{
			return $this->userInfoDAO->updateByCond($map, $cond, false, $params);
		}catch(Exception $e){
            $this->logger->error("updateInfoVal:".$e->getMessage());
			return false;
		}
	}
	/** 添加会员等级
	 * @author 2014-10-21 */
	public function addUserType($data){		
		$map = Tin :: createDataMap();
		$map->addEntry('TYPE_NAME', $data['type_name'], DB::VARCHAR);
		$map->addEntry('MONTHLY_RATE', $data['monthly_rate'], DB::DECIMAL);
		$map->addEntry('CREATE_DATE', 'now()', DB::DATETIME);
		try{
			$newTypeID = $this->userTypeDAO->insert($map, false, true);				
			return $newTypeID;
		}catch(Exception $e){
            $this->logger->error("addUserType:".$e->getMessage());
			return false;
		}
	}
	/** 修改会员等级
	 * @author 2014-10-21 */
	public function updateUserType($data, $cond){
            try{
                $map = Tin :: createDataMap();
		$map->addEntry('TYPE_NAME', $data['type_name'], DB::VARCHAR);
		$map->addEntry('MONTHLY_RATE', $data['monthly_rate'], DB::DECIMAL);
                $res = $this->userTypeDAO->updateByCond($map, $cond, false);

                return $res;
            } catch(Exception $e){
                $this->logger->error("updateUserType:".$e->getMessage());
                return false;
            }
	}
        
	/** 根据条件删除数据
	 * @param string $cond 条件
	 * @param array $params 绑定变量
	 * @author ZhangXiLian 2014-8-29 */
	public function delUser($cond, $params = false){
		return $this->userDAO->deleteByCond($cond, $params);
	}
	public function delUserInfo($cond, $params = false){
		return $this->userInfoDAO->deleteByCond($cond, $params);
	}
	public function delUserType($cond, $params = false){
		return $this->userTypeDAO->deleteByCond($cond, $params);
	}
        
        /** 根据制定条件获取符合条件的记录数量
         * 
         */
        public function getCountUser($cond){
            return $this->userDAO->getCountByCond($cond);
        }
//=========================================用户类型角色=============================================================
	/** 指定获取单条用户记录
	 * @author ZhangXiLian 2014-8-29 */
	public function getUserTypeItem($cols, $cond, $params = false){
		return $this->userTypeDAO->selectRowByCond($cols, $cond, $params);
	}

	
}//End Class
?>
